<?php

class GenerateCurrentDayCardBody
{

    private function getFinished(object $db, string $date) {

        $finished = $db->readFromFinished($date);

        if ($finished !== false) {
            $finished = json_decode($finished, true);
        }

        return $finished;

    }

    private function getText(object $db, string $date) {

        return $db->readFromSpecificDate($date);

    }

    public function getHtml(object $db, string $date)
    {

        // ---------------------------------------------------------------------

        $text = $this->getText($db, $date);
        $text = rtrim($text);
        $finished = $this->getFinished($db, $date);
        $html = "";
        $hrMinus = "<hr style='height 1px; border: 0; border-top: 2px dashed gray; margin-top: 4px; margin-bottom: 4px;' />";
        $hrEqual = "<hr style='height 1px; border: 0; border-top: 3px dashed darkorange; margin-top: 4px; margin-bottom: 4px;' />";

        // ---------------------------------------------------------------------

        if ($text == false || empty(trim($text))) {

            $html = "<span class='text-muted fw-lighter'>暂无，点击空白处编辑。</span>";

        } else {

            $str = str_replace(PHP_EOL, "<br />", $text);
            $arr = explode("<br />", $str);

            foreach ($arr as $item) {

                $item = trim($item);

                if (empty($item) || $item == "\r") {

                    $html .= "<br />";

                } elseif ($item == "暂无，点击空白处编辑。") {

                    $html .= "{$item}<br />";

                } elseif ($item[0] == "=" || $item[0] == "-") {
                    // 以 = 或 - 开头

                    if (empty(ltrim($item, "="))) {
                        // 全部由 = 组成

                        $item = $hrEqual;
                        $result .= $item;

                    } elseif (empty(ltrim($item, "-"))) {
                        // 全部由 - 组成

                        $item = $hrMinus;
                        $result .= $item;

                    } else {

                        $html .= <<<BBB
                            <input role="button" class="taskItem" type="checkbox" />
                            <span class="taskTitle py-1" role="button">$item</span>
                            <br />
                        BBB;

                    }

                } elseif ($item[0] == "#") {
                    // 以 # 开头

                    $lengthOfItemOld  = strlen($item); // item 字符串的长度，移除左侧 # 符号前的
                    $item = ltrim($item, "#"); // 移除左侧的 # 符号
                    $lengthOfItemNew = strlen($item); // item 字符串的长度，移除左侧 # 符号后的
                    $number = $lengthOfItemOld - $lengthOfItemNew; // 代表着有多少个 # 符号被移除

                    if (empty(trim($item))) {
                        // 移除左侧 # 符号后，什么字符都不剩下，或者只剩下了空格

                        $item = "<br />";

                    } else {

                        if (empty(trim(str_replace("=", "", $item)))) {
                            // 「# ======」

                            $item = $hrEqual;

                        } elseif (empty(trim(str_replace("-", "", $item)))) {
                            // 「# ------」

                            $item = $hrMinus;

                        } else {

                            if ($number == 1) {

                                $item = "<h5 class='fw-bold'>◆ {$item}</h5>";

                            } else {

                                $item = "<h6 class='fw-bold'>◇ {$item}</h6>";

                            }

                        }

                    }

                    $html .= $item;
                } else {

                    $clicked = "";
                    if ($finished !== false) {
                        if (in_array($item, $finished)) {
                            $clicked = "checked";
                        }
                    }

                    // 判断待办事项中是否包含中文或英文感叹号
                    $count = substr_count($item, "!") + substr_count($item, "！");
                    if ($count > 0) {
                        if ($count < 3) {
                            $item = "<span class='text-info'>" . $item . "</span>";
                        } else {
                            $item = "<span class='text-danger'>" . $item . "</span>";
                        }
                    } else {
                        // 判断待办事项中是否包含中文或英文问号
                        // 如果同时有叹号和问号，不论哪个在前哪个在后，都以叹号为准。
                        $count = substr_count($item, "?") + substr_count($item, "？");
                        if ($count > 0) {
                            $item = "<span class='text-muted fw-lighter'>" . $item . "</span>";
                        } else {
                            $item = "<span class='text-dark'>" . $item . "</span>";
                        }
                    }

                    $html .= <<<BBB
                        <input role="button" data-date="$date" class="taskItem" type="checkbox" $clicked />
                        <span class="taskTitle py-1" role="button">$item</span>
                        <br />
                    BBB;

                }
            }

        }

        // ---------------------------------------------------------------------

        return $html;

        // ---------------------------------------------------------------------

    }

}
